package com.ruigi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruigi.Exception.Customexception;
import com.ruigi.common.Basecontext;
import com.ruigi.mapper.Ordermapper;
import com.ruigi.pojo.*;
import com.ruigi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class Orderimpl extends ServiceImpl<Ordermapper, Orders> implements OrderService {
    @Autowired
    private ShopcartService shopcartService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderdetailService orderdetailService;


    @Override
    public void submit(Orders orders) {
        //获取当前用户id
        Long userid = Basecontext.getid();
        //查询当前用户的购物车数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userid);
        List<ShoppingCart> list = shopcartService.list(queryWrapper);
        if (list == null) {
            throw new Customexception("购物车不能为空");
        }
        //查询用户数据
        User byId = userService.getById(userid);
        //查询地址数据
        Long addressBookId = orders.getAddressBookId();
        AddressBook addressBook = addressService.getById(addressBookId);
        if (addressBook == null) {
            throw new Customexception("地址有误");
        }
        long id = IdWorker.getId();
        AtomicInteger atomicInteger = new AtomicInteger();
        List<OrderDetail> collect = list.stream().map(item -> {
            OrderDetail orderDetail = new OrderDetail();

            orderDetail.setOrderId(id);
            orderDetail.setNumber(item.getNumber());
            orderDetail.setDishFlavor(item.getDishFlavor());
            orderDetail.setDishId(item.getDishId());
            orderDetail.setSetmealId(item.getSetmealId());
            orderDetail.setName(item.getName());
            orderDetail.setImage(item.getImage());
            orderDetail.setAmount(item.getAmount());

            atomicInteger.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
            return orderDetail;


        }).collect(Collectors.toList());

        orders.setId(id);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setStatus(2);
        orders.setUserId(userid);
        orders.setAmount(new BigDecimal(atomicInteger.get()));
        orders.setNumber(String.valueOf(id));
        orders.setUserName(byId.getName());
        orders.setConsignee(addressBook.getConsignee());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress((addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
                + (addressBook.getCityName() == null ? "" : addressBook.getCityName())
                + (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
                + (addressBook.getDetail() == null ? "" : addressBook.getDetail()));
        //插入一条数据
        orderService.save(orders);

        orderdetailService.saveBatch(collect);

        //清空购物车
        shopcartService.remove(queryWrapper);

    }
}
